/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				ResourceInfrastructureGoal
 
Function						: 				Provides a class for holding the research infrastructure
												goal
****************************************************************************/
package planninglanguage.goals;

import org.jdom.Element;

import planninglanguage.conditions.AndCondition;
import planninglanguage.conditions.Condition;
import planninglanguage.conditions.NumberOfUnitsOfTypeCondition;
import planninglanguage.softconditions.AbsoluteNumberOfUnitsOfTypeSoftCondition;
import planninglanguage.softconditions.AndSoftCondition;
import planninglanguage.softconditions.SoftCondition;
import util.XMLWriter;
import base.WargusGameState;

public class ResourceInfrastructureGoal extends Goal implements Cloneable{

	int m_playerID;
	int m_numPeasants;
	int m_numFarms;
	
	public ResourceInfrastructureGoal(int a_playerID, int a_numPeasants, int a_numFarms) {
		m_playerID = a_playerID;
		m_numPeasants = a_numPeasants;
		m_numFarms = a_numFarms;
	}
	
	public int unitID()
	{
		return m_playerID;
	}
	
	public Object clone()
	{
		return new ResourceInfrastructureGoal(m_playerID,m_numPeasants,m_numFarms);
	}
	
	public Condition generateSuccessCondition(WargusGameState a_gamestate)
	{
		AndCondition ac = null;
		NumberOfUnitsOfTypeCondition peasants = new NumberOfUnitsOfTypeCondition("peasant",m_playerID,m_numPeasants);
		// Note: This code assumes that the player is human:
		NumberOfUnitsOfTypeCondition farms = new NumberOfUnitsOfTypeCondition("farm",m_playerID,m_numFarms);
		ac = new AndCondition(peasants,farms);
		return ac;
	}
	
	public SoftCondition generateSuccessSoftCondition(WargusGameState a_gamestate)
	{
		AndSoftCondition ac = null;
		AbsoluteNumberOfUnitsOfTypeSoftCondition peasants = new AbsoluteNumberOfUnitsOfTypeSoftCondition("peasant",m_playerID,m_numPeasants);
		// Note: This code assumes that the player is human:
		AbsoluteNumberOfUnitsOfTypeSoftCondition farms = new AbsoluteNumberOfUnitsOfTypeSoftCondition("farm",m_playerID,m_numFarms);
		ac = new AndSoftCondition(peasants,farms);
		return ac;
	}	
	
	public float distance(Goal a_g) {
		if (!(a_g instanceof ResourceInfrastructureGoal)) return 1;
		
		ResourceInfrastructureGoal g = (ResourceInfrastructureGoal) a_g;
		float d_np;
		float d_nf;

		d_np = Math.min(5,Math.abs(m_numPeasants - g.m_numPeasants)) / 5.0f;
		d_nf = Math.min(5,Math.abs(m_numFarms - g.m_numFarms)) / 5.0f;
			
		return (d_np + d_nf)/2.0f;
	}
	
	public String toString() {
		return "ResourceInfrastructureGoal( peasants = " + m_numPeasants + " farms = " + m_numFarms + ")";
	}
	
	public void writeXML(XMLWriter w) {
		w.tag(Goal.openTag + " " + Goal.XMLtype + "=\"" + 
				this.getClass().getSimpleName() + "\"");
		{
			w.tag(Goal.XMLplayerID, m_playerID);
			w.tag("peasants", m_numPeasants);
			w.tag("farms", m_numFarms);
		}
		w.tag(Goal.closeTag);
	}	
	protected static ResourceInfrastructureGoal loadFromXML(Element e) {
		int playerID = Integer.parseInt(e.getChildText(XMLplayerID));
		int numPeasants = Integer.parseInt(e.getChildText("peasants"));
		int numFarms = Integer.parseInt(e.getChildText("farms"));
		return new ResourceInfrastructureGoal(playerID, numPeasants, numFarms);
	}
}
